package com.tjbklx1.beanutils;

import java.util.Date;

import org.apache.commons.beanutils.Converter;

/***
 * 自定义时间转换
 * ClassName:CustomDateConvert.java
 * @author Sam Tan
 * @Description TODO
 * @date 2017年11月28日
 */
public class CustomDateConvert implements Converter{
	
	final static String SPACE = " ";
	

	@SuppressWarnings("unchecked")
	public <T> T convert(Class<T> type, Object value) {
		
    	if(Date.class.isAssignableFrom(value.getClass()) && String.class.isAssignableFrom(type)){
    		value = DateUtil.dateToStr((Date)value,DateUtil.DATE_FORMAT_LONG);
    	}else if(Date.class.isAssignableFrom(type) && String.class.isAssignableFrom(value.getClass())){
    		
    		String converDate = String.valueOf(value);
    		converDate = converDate.indexOf(SPACE) == -1?converDate + " 00:00:00" : converDate;
    		value = DateUtil.strToDate(DateUtil.DATE_FORMAT_LONG, converDate);
    	}
		return (T) value;
	}

}
